﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ServicesSheet_StatSelfCarFeeTemp : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            

            SetDataSource();

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/私车公用车辆费用统计.htm";
            master.PageName = "私车公用车辆费用统计";
            master.PicUrl = "";
            master.IntiBar();
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SetDataSource();
    }

    private void SetDataSource()
    {
        string Sql = "";
        string strYear = "";
        string strMonth = "";

        strYear = DSelYearMonth1.SelectYear;
        strMonth = DSelYearMonth1.SelectMonth;
        if (strYear == "0" || strYear == string.Empty || strYear == "") strYear = DateTime.Now.Year.ToString();
        
        if (strYear.Length == 0)
        {
            strMonth = "";
        }
        else
        {
            if (strMonth == "0" || strMonth == string.Empty) strMonth = "";
        }

        Sql = Sql + "  SELECT Services_SV_CarSubsidy.EmployeeId, Services_SV_CarSubsidy.Years, ";
        Sql = Sql + "  Services_SV_CarSubsidy.PerKMSubsidy, Employees.EmpName,";
        Sql = Sql + " (SELECT SUM(Services_SCGY.UseKil)";
        Sql = Sql + "  FROM Services_SCGY";
        Sql = Sql + " WHERE (Services_SCGY.EmployeeId = Services_SV_CarSubsidy.EmployeeId)  ";
        if (strYear.Length > 0) Sql = Sql + string.Format(" AND (YEAR(Services_SCGY.UDate) = {0}) ", strYear);
        if (strMonth.Length > 0) Sql = Sql + string.Format(" AND (MONTH(Services_SCGY.UDate) = {0})", strMonth);
        Sql = Sql + "  GROUP BY Services_SCGY.EmployeeId) AS AllKill,";
        Sql = Sql + " (SELECT SUM(Services_SCGY.SupExpenses)";
        Sql = Sql + " FROM Services_SCGY";
        Sql = Sql + " WHERE (Services_SCGY.EmployeeId = Services_SV_CarSubsidy.EmployeeId) ";
        if (strYear.Length > 0) Sql = Sql + string.Format("  AND (YEAR(Services_SCGY.UDate) = {0}) ", strYear);
        if (strMonth.Length > 0) Sql = Sql + string.Format("  AND (MONTH(Services_SCGY.UDate) = {0})", strMonth);
        Sql = Sql + "  GROUP BY Services_SCGY.EmployeeId) AS AllSExpenses";
        Sql = Sql + " FROM Services_SV_CarSubsidy INNER JOIN";
        Sql = Sql + "  Employees ON ";
        Sql = Sql + " Services_SV_CarSubsidy.EmployeeId = Employees.EmployeeID";
        //if (strYear.Length > 0) Sql = Sql + string.Format(" WHERE (Services_SV_CarSubsidy.Years = {0})", strYear);

        DataSet Tmp;
        InitializeComponent();
        Tmp = WMaster.Execute(Sql);

        DataTable svTable = Tmp.Tables[0];
        svTable.Columns.Add(new DataColumn("Amount", typeof(Int32)));
        int AllKillCount = 0;
        Single AllAmount = 0;
        int AllExpenses = 0;

        for (int i = 0; i < svTable.Rows.Count; i++)
        {
            int AKil = (svTable.Rows[i]["AllKill"] == Convert.DBNull) ? 0 : Convert.ToInt32(svTable.Rows[i]["AllKill"]);
            int ASExpenses = (svTable.Rows[i]["AllSExpenses"] == Convert.DBNull) ? 0 : Convert.ToInt32(svTable.Rows[i]["AllSExpenses"]);
            Single PerKMSubsidy = (svTable.Rows[i]["PerKMSubsidy"] == Convert.DBNull) ? 0 : Convert.ToSingle(svTable.Rows[i]["PerKMSubsidy"]);
            Single Amout = (AKil * PerKMSubsidy) + ASExpenses;
            svTable.Rows[i]["Amount"] = Amout;

            AllKillCount = AllKillCount + AKil;
            AllExpenses = AllExpenses + ASExpenses;
            AllAmount = AllAmount + Amout;
        }

        DataRow SR;
        SR = svTable.NewRow();

        SR["EmpName"] = "合计:";
        SR["AllKill"] = AllKillCount;
        SR["AllSExpenses"] = AllExpenses;
        SR["Amount"] = AllAmount;

        svTable.Rows.Add(SR);

        wgvMaster.DataSource = svTable;
        wgvMaster.DataBind();

    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_StatSelfCarFeeTemp));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = false;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.SV_StatEmp";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

}
